package com.medaxis.hpams.sso.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.medaxis.hpams.dto.global.BaseResponse;
import com.medaxis.hpams.dto.global.ErrorException;
import com.medaxis.hpams.dto.global.SuccessResponse;

import com.medaxis.hpams.sso.constant.Constants;
import com.medaxis.hpams.sso.domain.dto.PageDomain;
import com.medaxis.hpams.sso.domain.dto.PostDto;
import com.medaxis.hpams.sso.domain.dto.TableDataInfo;
import com.medaxis.hpams.sso.domain.dto.TableSupport;

import com.medaxis.hpams.sso.service.PostService;
import com.medaxis.hpams.sso.utils.ResponseUtils;
import com.medaxis.hpams.sso.utils.SqlUtil;
import com.medaxis.hpams.sso.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(Constants.VERSION_NUMBER+"/sso/post")
@Api(tags = Constants.VERSION_NUMBER+ "职位管理")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private ResponseUtils responseUtils;


    /**
     * 获取岗位列表
     */
    @ApiOperation(value = "按岗位信息条件进行模糊查找")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功",response = PostDto.class)})
    @GetMapping("/list")
    public BaseResponse list(PostDto post) {
        startPage();
        List<PostDto> list = postService.selectPostList(post);
        return new SuccessResponse(getDataTable(list));
    }


    /**
     * 删除岗位
     */
    @ApiOperation(value = "根据id批量删除岗位信息")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功",response = int.class)})
    @DeleteMapping("/{postIds}")
    public BaseResponse remove(@PathVariable Long[] postIds) {
        return responseUtils.toResponse(postService.deletePostByIds(postIds));
    }

    /**
     * 新增岗位
     */
    @ApiOperation(value = "添加岗位信息")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功",response = int.class)})
    @PostMapping("/add")
    public BaseResponse add(@Validated @RequestBody PostDto post) {
        if (Constants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            throw new ErrorException("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (Constants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            throw new ErrorException("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
//        post.setCreateBy(SecurityUtils.getUsername());
        return responseUtils.toResponse(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @ApiOperation(value = "修改岗位信息")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功",response = int.class)})
    @PutMapping("/edit")

    public BaseResponse edit(@Validated @RequestBody PostDto post) {
        if (Constants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            throw new ErrorException("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (Constants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            throw new ErrorException("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
//        post.setUpdateBy(SecurityUtils.getUsername());
        return responseUtils.toResponse(postService.updatePost(post));
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.OK.value());
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }



}
